<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { deletePatientAPI, getPatientListAPI } from '@/api/patients'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'
  import { onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])
  // 患者列表
  const patientList = ref<PatientItem[]>([])
  //获取患者数据
  onShow(async () => {
    const res = await getPatientListAPI()
    patientList.value = res
  })
  //删除患者
  const onSwiperDel = async (id: string, index: number) => {
    await deletePatientAPI(id)
    uni.showToast({
      title: '删除成功',
      icon: 'success',
    })
    if (store.patientId === id) {
      store.patientId = ''
    }
    patientList.value.splice(index, 1)
  }
  //接收页面跳转传过来的数据
  interface IParams {
    type: 'consult'
  }
  const params = defineProps<IParams>()
  // 选择患者
  const onSelect = (id: string) => {
    if (!params.type) return
    store.patientId = id
  }
  // 设置页面标题
  if (params.type === 'consult') {
    uni.setNavigationBarTitle({
      title: '选择患者',
    })
  }
  const store = useConsultStore()
  //下一步逻辑函数
  const go = () => {
    uni.navigateTo({
      url: '/subpkg_consult/payment/payment',
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <!--  患者列表 -->
      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="(item, index) in patientList"
          :key="item.id"
          @click="onSwiperDel(item.id!, index)"
        >
          <view
            @click="onSelect(item.id!)"
            class="archive-card"
            :class="{ active: store.patientId === item.id && params.type === 'consult' }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">
                {{ item.idCard!.replace(/(\d{3})(\d{12})(\d{3})/, '$1' + '*'.repeat(12) + '$3') }}
              </text>
              <text class="default" v-if="item.defaultFlag">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="true" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>

      <!-- 下一步按钮 -->
      <view v-if="type === 'consult' && store.patientId !== ''">
        <button @click="go" class="uni-button">下一步</button>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './list.scss';
  .uni-button {
    background-color: #16c2a3;
    border-radius: 46rpx;
  }
</style>
